package com.xiaoniu.forum.web.controller.app;

import com.xiaoniu.forum.common.utils.R;
import com.xiaoniu.forum.renren.common.exception.RRException;
import com.xiaoniu.forum.renren.modules.oss.cloud.OSSFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UploadController {
    @PostMapping("/app/upload")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
        return R.ok().put("url", url);
    }
}
